/*
 * Copyright 2005-2010 the original author or authors.
 * 
 *      ruixun
 *
 * Project csmp
 */
package com.csmp.service.impl;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.bs.bean.Certificate;
import com.csmp.service.AbstractCertificateService;

/**
 * @author lizhiqiang
 * @version 2011-2-22
 * @since JDK 1.5
 */
public class CertificateServiceImpl extends AbstractCertificateService {

	protected void delObj(Certificate entity) {
		getHibernateTemplate().delete(entity);

	}

	protected Certificate loadObj(Serializable id) {
		return (Certificate) getHibernateTemplate().get(CLASS_NAME, id);
	}

	protected void saveObj(Certificate entity) {
		if(StringUtils.isBlank(entity.getId())){
			getHibernateTemplate().save(entity);
		}else
		 getHibernateTemplate().update(entity);
	}

	@SuppressWarnings("unchecked")
	protected List<Certificate> searchList(Certificate entity,int first,int max) {
		DetachedCriteria criteria = getCriteria();
		try {
			setContion(entity, criteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<Certificate> list = getHibernateTemplate().findByCriteria(criteria,first,max);
		return list;
	}

	private DetachedCriteria getCriteria() {
		return DetachedCriteria.forClass(CLASS_NAME);
	}
	
	@SuppressWarnings("unchecked")
	public int getCount(Certificate bean) {
		DetachedCriteria criteria=getCriteria();
		try {
			setContion(bean,criteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		criteria.setProjection(Projections.rowCount());
		List<Integer> list=getHibernateTemplate().findByCriteria(criteria);
		if(list == null || list.isEmpty()){
			return 0;
		}
		return list.get(0);
	}
	
	private void setContion(Certificate entity,DetachedCriteria criteria)throws Exception{
		if(StringUtils.isNotBlank(entity.getCode())){
			criteria.add(Expression.like("code", "%"+entity.getCode()+"%"));
		}
		
		if(StringUtils.isNotBlank(entity.getCompanyName())){
			criteria.add(Expression.like("companyName", "%"+entity.getCompanyName()+"%"));
		}
		
		if(StringUtils.isNotBlank(entity.getTypes())){
			criteria.add(Expression.like("types", "%"+entity.getTypes()));
		}
		/*
		if(null !=entity.getValidDate()){
			criteria.add(Expression.le("validDate", entity.getValidDate()));
		}*/
		
		if(StringUtils.isNotBlank(entity.getKs())){
			criteria.add(Expression.like("ks", entity.getKs(),MatchMode.START));
		}
		if(StringUtils.isNotBlank(entity.getXm())){
			if(entity.isSfgly())
			 criteria.add(Expression.like("xm", entity.getXm().trim(),MatchMode.ANYWHERE));
			else{
			 criteria.add(Expression.in("xm", entity.getXm().split(",")));
			}
		}else if(StringUtils.isNotBlank(entity.getXmSerach())){
			criteria.add(Expression.like("xm", entity.getXmSerach().trim(),MatchMode.ANYWHERE));
		}else{
			if(!entity.isSfgly())
			 criteria.add(Expression.eq("xm", null));
				
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		if(StringUtils.isNotBlank(entity.getApprovlDate1()) && StringUtils.isNotBlank(entity.getApprovlDate2())){
			criteria.add(Restrictions.between("createDate", sdf.parse(entity.getApprovlDate1()),sdf.parse(entity.getApprovlDate2())));
		}else if(StringUtils.isNotBlank(entity.getApprovlDate1())){
			criteria.add(Restrictions.ge("createDate",sdf.parse(entity.getApprovlDate1())));
		}else if(StringUtils.isNotBlank(entity.getApprovlDate2())){
			criteria.add(Restrictions.lt("createDate", sdf.parse(entity.getApprovlDate2())));
		}
		
		if(StringUtils.isNotBlank(entity.getYqCode())){
			criteria.add(Restrictions.like("yqCode", entity.getYqCode().trim(),MatchMode.ANYWHERE));
		}
		
		
	}

}
